<?php
/**
 * Created by PhpStorm.
 * User: david
 * Date: 2018/6/20
 * Time: 9:15
 */

namespace app\admin\controller;


use app\admin\Controller;
use app\common\model\Xmdj as XmdjModel;
use app\common\model\XmdjImg as XmdjImgModel;
use traits\controller\Jump;

class Xmdj extends Controller
{
    use \app\admin\traits\controller\Controller;
    use Jump;

    public function addxmdj()
    {
        $data = $this->request->param()['data'];
        $data['desc'] = urldecode($data['desc']);
        $time = time();
        $data['create_time'] = $time;
        $data['update_time'] = $time;
        XmdjModel::create($data);
        return [
            'code' => 0,
            'msg' => 'success'
        ];
    }

    public function edit()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->param()['data'];
            $data['desc'] = urldecode($data['desc']);
            $time = time();
            $data['update_time'] = $time;
            XmdjModel::update($data);
            return [
                'code' => 0,
                'msg' => 'success'
            ];
        } else {
            $id = $this->request->param('id');
            $xmdj = XmdjModel::get($id);
            $this->view->assign('xmdj', $xmdj);
            return $this->view->fetch('editxmdj');
        }
    }

    public function imgs()
    {
        if ($this->request->isGet()) {
            $imgs = XmdjImgModel::getImgsByPid($this->request->param('id'));
            $this->view->assign('id', $this->request->param('id'));
            $this->view->assign('imgs', $imgs);
            return $this->view->fetch('img');
        } else {
            $data = $this->request->param();
            XmdjImgModel::create($data);
            return $this->success('添加成功');
        }
    }

    public function imgdel()
    {
        $id = $this->request->param('id');
        XmdjImgModel::destroy($id);
        return ajax_return_adv('删除成功', '');
    }
}